// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false;

Vue.use(ElementUI);

// Vue.prototype.$url = 'http://zhzl.5niu.top';
Vue.prototype.$url = 'http://zhzl.5niu.top';
axios.defaults.baseURL='http://www.zhzlsp.com';//axios基础url
Vue.prototype.$axios=axios;
Vue.prototype.$back = function () {
    this.$router.history.go(-1);
};
Vue.prototype.$telReg = function (str) {
    var telReg = /^[1][3,4,5,7,8][0-9]{9}$/;
    return telReg.test(str.trim())
};
Vue.prototype.$pwdReg = function(str){
    var pwdrge = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,21}$/;//密码：6-21位，不能是纯数字或纯字母
    return pwdrge.test(str.trim())
}

Vue.prototype.toLogin = function ($this) {
    // $this.$toast('请注册');
    setTimeout( ()=> {
        // $this.$router.push('/register');
    },500)
};
Vue.prototype.$addIdea = function (username,tel,text,callback,toLogin) {
    var v = this;
    if(!username.trim()){
        this.$message.error('请输入您的姓名！');
        return
    }
    if(!tel.trim()){
        this.$message.error('请输入手机号！');
        return
    }
    if(!text.trim()){
        this.$message.error('请输入意见或建议！');
        return
    }
    if(!v.$telReg(tel.trim())){
        this.$message.error('请输入正确的手机号！');
        return
    }
    const loading = this.$loading({
        lock: true,
        text: '提交中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.4)'
    });
    var v = this ,
        params = new FormData();
    params.append('name',username.trim());
    params.append('content',text.trim());
    params.append('mobile',tel.trim());
    v.$axios.post('/index.php?g=portal&m=Fankui&a=add',params)
        .then((res) => {
            res = res.data;
            loading.close();

            if (res.result == 1) {
                v.$message.success(res.msg || '提交成功！');
                callback();
            } else if(res.result == -100){
                v.$message.error(res.msg || '请登录！');
                toLogin();
            }else {
                v.$message.error(res.msg || '提交失败！');
            }
        }).catch((err) => {
        loading.close();
        v.$message.error('提交失败！');
    });
};
Vue.prototype.$ExpectedTrends = function (status,id,callback) {
    var text = '';
    if(status == 1 ){
        text = '确定看涨吗?'
    }else if(status == 2){
        text = '确定看跌吗?'
    }else{
        text = '确定看平吗?'
    }
    this.$alert(text, '趋势', {
        confirmButtonText: '确定',
        callback: action => {
            console.log(action);
            if(action == 'confirm'){
                const loading = this.$loading({
                    lock: true,
                    text: '提交中...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.4)'
                });
                var v = this ,
                    params = new FormData();
                params.append('status',status);
                params.append('id',id);
                v.$axios.post('/index.php?g=portal&m=index&a=update_up_down',params)
                    .then((res) => {
                        res = res.data;
                        loading.close();
                        if (res.result == 1) {
                            v.$message.success(res.msg || '提交成功！');
                            callback();
                        }else {
                            v.$message.error(res.msg || '提交失败！');
                        }
                    }).catch((err) => {
                    loading.close();
                    v.$message.error('提交失败！');
                });
            }
        }
    });
}

Vue.prototype.$removeHTMLTag = function(str) {
    str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
    str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
    //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    str=str.replace(/ /ig,'');//去掉
    return str;
}

import commons from '../static/js/__commons__';//全局变量
Vue.prototype.$commons=commons;


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
});
